<?php
	include('connect.php');
	$message="";
	if(isset($_POST['type'])){
		if($_POST['app']=='event_reminder_users'){
			$type='ER';
		}else if($_POST['app']=='family_safety_users'){
			$type='FSU';
		}else if($_POST['app']=='user_info'){
			$type='MAT';
		}else if($_POST['app']=='friends'){
			$type='FRIENDS';
		}
		
		$email=$_POST['email'];
		if($_POST['app']=='user_info'){
			$sql=mysql_query("select ".$_POST['email'].",secondary_mail_id from ".$_POST['app']."");
		}else{
			$sql=mysql_query("select ".$_POST['email']." from ".$_POST['app']."");
		}
		while($row=mysql_fetch_array($sql)){//$count++;
			if($_POST['app']=='user_info'){
				$sql2="select count(*) from users_emails where email='".$row[$email]."' and type='".$type."'";
				$res=mysql_query($sql2,$con);
				$row1=mysql_fetch_array($res);
				if($row1[0]=='0'){
					$sql3="insert into users_emails(email,added_date,type,status)values('".$row[$email]."',NOW(),'".$type."',1)";
					$res2=mysql_query($sql3,$con);
				}
				$sql4="select count(*) from users_emails where email='".$row['secondary_mail_id']."' and type='".$type."'";
				echo $sql4;
				$ress=mysql_query($sql4,$con);
				$roww1=mysql_fetch_array($ress);
				if($roww1[0]=='0'){
					$sql5="insert into users_emails(email,added_date,type,status)values('".$row['secondary_mail_id']."',NOW(),'".$type."',1)";
					$res3=mysql_query($sql5,$con);
				}
			}else{
				$sql2="select count(*) from users_emails where email='".$row[$email]."' and type='".$type."'";
				$res=mysql_query($sql2,$con);
				$row1=mysql_fetch_array($res);
				if($row1[0]=='0'){
					$sql3="insert into users_emails(email,added_date,type,status)values('".$row[$email]."',NOW(),'".$type."',1)";
					$res2=mysql_query($sql3,$con);
				}
			}
		}
		$message="Added to user emails table";
		
	
	}
 ?>
<style>
	select{
		border:1px solid #ccc;
		height:34px;
		padding-left:10px;
		text-align:left;
		margin-right:20px;
	}
</style>
<?php include('header.php')?>
<html>
	<head>
        <script src="js/datatablejquery.js"></script>
	</head>
    <body>
		<center>
			<form id="formid" method="post" action="">
				<div>
				Select App<select id="app" name="app" onChange="return usersList()">
								<option value="" >select app</option>
								<option value="event_reminder_users">ER</option>
								<option value="family_safety_users">FSU</option>
								<option value="user_info">MAT</option>
								<option value="friends">FRIENDS</option>
							</select>
				</div>
				<br/>
				<br/>
				<input type="hidden" id="email" name="email">
				<br/><br/><br/>
				<div style="text-align:center;">
					 <input type="button" value="Go" onClick="return userTransfer();">
				</div>
				<input type="hidden" id="type" name="type" value="0">
			</form>
			<?php echo $message; ?>
		</center>	
    </body>
</html>
<?php include('footer.php')?>
<script>
function userTransfer(){
		var app=$("#app").val();
		if(app==""){
			alert('select app');return false;
		}
		$("#formid").submit();
}
function usersList(){
		var app=$("#app").val();
		if(app==""){
			alert('select app');return false;
		}
		if(app=='event_reminder_users'){
			var email=$("#email").val('email_id');
		}else if(app=='family_safety_users'){
			var email=$("#email").val('email_id');
		}else if(app=='user_info'){
			var email=$("#email").val('primary_mail_id');
		}else if(app=='friends'){
			var email=$("#email").val('email_id');
		}
}
</script>